layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;



    //监听单元格编辑
    table.on('edit(btyZxYusuanTempTable)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        $.ajax({
            type: "PUT",
            url: "/btyZxYusuanTemp/editItem",
            datatype: "json",
            data: data,
            success: function (data) {
            Feng.success("修改成功！");
                table.reload(BtyZxYusuanTemp.tableId);
            }, error: function () {
                Feng.error("修改失败！");
            }, complete: function (XHR, TS) {
                //layer.close(layerIndex);
                //50秒自动刷新一次
                //setTimeout(showTime,50000);
            }

        });
        //layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
    });

    /**
     * 管理
     */
    var BtyZxYusuanTemp = {
        tableId: "btyZxYusuanTempTable"
    };

    /**
     * 初始化表格的列
     */
    BtyZxYusuanTemp.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'xuhao', sort: true, title: '序号',edit: 'text', width: 70},
            {field: 'xiangmu', sort: true, title: '项目名称',edit: 'text'},
            // {field: 'createUser', sort: true, title: ''},
            // {field: 'updateUser', sort: true, title: ''},
            // {field: 'updateUserName', sort: true, title: ''},
            // {field: 'updateTime', sort: true, title: ''},
            {field: 'sjdw', sort: true, title: '时间单位',edit: 'text'},
            {field: 'danjia', sort: true, title: '单价',edit: 'text'},
            {field: 'jine', sort: true, title: '金额',edit: 'text'},
            {field: 'createUserName', sort: true, title: '创建人'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyZxYusuanTemp.search = function () {
        var queryData = {};

        queryData['xiangmu'] = $('#xiangmu').val();
        queryData['sjdw'] = $('#sjdw').val();

        table.reload(BtyZxYusuanTemp.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyZxYusuanTemp.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyZxYusuanTemp/add',
            tableId: BtyZxYusuanTemp.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyZxYusuanTemp.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyZxYusuanTemp/edit?id=' + data.id,
              tableId: BtyZxYusuanTemp.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyZxYusuanTemp.exportExcel = function () {
        var checkRows = table.checkStatus(BtyZxYusuanTemp.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyZxYusuanTemp.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyZxYusuanTemp/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyZxYusuanTemp.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyZxYusuanTemp.tableId,
        url: Feng.ctxPath + '/btyZxYusuanTemp/list',
        page: true,
        cellMinWidth: 100,
        cols: BtyZxYusuanTemp.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyZxYusuanTemp.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyZxYusuanTemp.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyZxYusuanTemp.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyZxYusuanTemp.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyZxYusuanTemp.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyZxYusuanTemp.onDeleteItem(data);
        }
    });
});
